package com.example.book.base;

import com.example.book.common.shiro.ShiroUtils;
import com.example.book.domain.sys.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;

/**
 * 视图控制器基类
 *
 * @author haocj
 */
@Controller
public class BaseController {

    /**
     * 日志对象
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());

    private static String[] datePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
            "yyyy.MM.dd HH:mm", "yyyy.MM"};


    /**
     * 将前台传递过来的日期格式的字符串，自动转化为Date类型
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        // Date 类型转换
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                try {
                    if(StringUtils.isNotBlank(text)){
                        setValue(DateUtils.parseDate(text, datePatterns));
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });

        // Timestamp 类型转换
        binder.registerCustomEditor(Timestamp.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                Date date;
                try {
                    date = DateUtils.parseDate(text, datePatterns);
                    setValue(date == null ? null : new Timestamp(date.getTime()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });

        // Timestamp 类型转换
        binder.registerCustomEditor(Timestamp.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                Date date;
                try {
                    date = DateUtils.parseDate(text, datePatterns);
                    setValue(date == null ? null : new Timestamp(date.getTime()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 下面返回ajax信息
     * @return
     */
    public Result success() {
        return Result.success();
    }

    public Result success(String msg) {
        return Result.success(msg);
    }

    public Result success(Object object) {
        return Result.success(object);
    }

    public Result error() {
        return Result.error();
    }

    public Result error(Integer code) {
        return Result.error(code);
    }

    public Result error(Integer code, String msg) {
        return Result.error(code, msg);
    }

    /**
     * 获取session 用户
     * @return
     */
    public SysUser getUser() {
        return ShiroUtils.getUser();
    }

    /**
     * 获取用户id
     * @return
     */
    public String getUserId() {
        return getUser().getId();
    }
}
